package com.xinyun.traceability.controller;

import com.github.pagehelper.PageInfo;
import com.xinyun.traceability.enity.dto.CommodityDTO;
import com.xinyun.traceability.enity.dto.PageQueryResult;
import com.xinyun.traceability.enity.po.Commodity;
import com.xinyun.traceability.enity.po.CommodityPic;
import com.xinyun.traceability.enity.po.SysUser;
import com.xinyun.traceability.service.CommodityPicService;
import com.xinyun.traceability.service.CommodityService;
import com.xinyun.traceability.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class BusinessController {

    @Autowired
    CommodityService commodityService;

    @Autowired
    CommodityPicService commodityPicService;

    /**
     * 进入商品列表
     * @return
     */
    @GetMapping("/commodity/list")
    public String business() {
        return "commodity/list";
    }

    /**
     * 查询商品列表
     * @param session
     * @param commodityDTO
     * @return
     */
    @ResponseBody
    @RequestMapping("/commodity/queryCommodityPage")
    public PageQueryResult queryCommodityPage(HttpSession session, CommodityDTO commodityDTO) {
        SysUser sysUser = (SysUser) session.getAttribute("sysUser");
        commodityDTO.setBusinessId(sysUser.getId());
        PageInfo<Commodity> page = commodityService.queryCommodityPage(commodityDTO);
        PageQueryResult pageQueryResult = new PageQueryResult(page, "", true, "0");
        return pageQueryResult;
    }

    /**
     * 进入商品添加页面
     * @param request
     * @return
     */
    @GetMapping("/commodity/add")
    public String toadd(HttpServletRequest request) {
        request.setAttribute("uuid", UUID.randomUUID().toString().replaceAll("-", ""));
        return "commodity/add";
    }

    /**
     * 保存商品
     * @param commodity
     * @return
     */
    @ResponseBody
    @PostMapping("/commodity/add")
    public R add(Commodity commodity) {
        try {
            commodityService.saveCommodity(commodity);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("保存失败");
        }
        return R.succeed("保存成功");
    }

    /**
     * 进入商品编辑页面
     * @param request
     * @return
     */
    @GetMapping("/commodity/{id}/edit")
    public String toedit(HttpServletRequest request, @PathVariable Integer id) {
        Commodity commodity = commodityService.selectCommodityByPrimaryKey(id);
        List<CommodityPic> commodityPicList = commodityPicService.selectCommodityPicByUuid(commodity.getUuid());
        request.setAttribute("commodity", commodity);
        request.setAttribute("commodityPicList", commodityPicList);
        request.setAttribute("uuid", commodity.getUuid());
        return "commodity/add";
    }

    /**
     * 商品图片上传
     * @param uuid
     * @param request
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/{uuid}/commodityPicUpload", method = RequestMethod.POST)
    public R commodityPicUpload(@PathVariable String uuid, HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        if (multipartFile.getSize() > 1024 * 1024 * 10) {
            return R.failed("文件太大");
        }
        List<MultipartFile> multipartFiles = new ArrayList<MultipartFile>();
        multipartFiles.add(multipartFile);
        return commodityPicService.layuiUpload(uuid, multipartFiles, request);
    }

    /**
     * 扫描二维码查看商品信息
     * @param request
     * @param uuid
     * @return
     */
    @GetMapping("/commodity/{uuid}/verify")
    public String commodityVerify(HttpServletRequest request, @PathVariable String uuid) {
        Commodity commodity = commodityService.selectCommodityByUuid(uuid);
        List<CommodityPic> commodityPicList = commodityPicService.selectCommodityPicByUuid(uuid);
        Commodity record = new Commodity();
        record.setId(commodity.getId());
        record.setScanCount(commodity.getScanCount() + 1);
        commodityService.updateByPrimaryKeySelective(record);
        request.setAttribute("commodity", commodity);
        request.setAttribute("commodityPicList", commodityPicList);
        return "commodity/verify";
    }

    /**
     * 显示商品图片
     * @param id
     * @param response
     */
    @GetMapping(value = "/commodityPic/{id}/view")
    public void commodityPicView(@PathVariable Integer id, HttpServletResponse response) {
        commodityPicService.commodityPicView(response, id);
    }

    @GetMapping("/commodity/search")
    public String tosearch() {
        return "customer/search";
    }

    @PostMapping("/commodity/search")
    public String search(HttpServletRequest request, @RequestParam(required = true) String name, String optype) {
        CommodityDTO commodityDTO = new CommodityDTO();
        commodityDTO.setName(name);
        List<Commodity> commodityList = commodityService.selectCommodityByCommodityDTO(commodityDTO);
        request.setAttribute("commodityList", commodityList);
        request.setAttribute("name", name);
        request.setAttribute("optype", optype);
        return "customer/search";
    }

    /**
     * 状态删除商品
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping("/commodity/{id}/delete")
    public R deleteCommodity(@PathVariable Integer id) {
        try {
            Commodity commodity = new Commodity();
            commodity.setUpdateTime(new Date());
            commodity.setValid(0);
            commodity.setId(id);
            commodityService.updateByPrimaryKeySelective(commodity);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("删除失败");
        }
        return R.succeed("操作成功");
    }

    /**
     * 删除商品图片
     * @param id
     * @return
     */
    @ResponseBody
    @DeleteMapping("/commodityPic/{id}/delete")
    public R deleteCommodityPic(@PathVariable Integer id) {
        try {
            commodityPicService.deleteCommodityPicByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("删除失败");
        }
        return R.succeed("操作成功");
    }
}
